package 基础;

import java.nio.charset.Charset;
import java.util.Arrays;

public class 编码转换 {
	public static void main(String[] args) {
			final String hexString = "0123456789ABCDEF";
		    final char[] hexCode = "0123456789ABCDEF".toCharArray();

			/**
			 * int -> byte[]
			 */
			int ii=160000000;
			byte[] bi=new byte[4];
//			bi[0]=(byte) ((ii>>24)&0xff);
//			bi[1]=(byte) ((ii>>16)&0xff);
//			bi[2]=(byte) ((ii>>8)&0xff);
//			bi[3]=(byte) (ii&0xff);
			for(int j=bi.length-1;j>=0;j--) {//等同上
				bi[j]=Integer.valueOf(ii & 0xff).byteValue();
				ii>>=8;
			}
			System.out.println(Arrays.toString(bi));//[9, -119, 104, 0]  [00001001 10001001 01101000 00000000]
			
			/**
			 * byte[] -> int
			 */
			ii=((bi[0]&0xFF)<<24)|((bi[1]&0xFF)<<16)|((bi[2]&0xFF)<<8)|((bi[3])&0xFF);
			System.out.println(ii);//160000000
			System.out.println(Integer.toBinaryString(ii));//00001001 10001001 01101000 00000000


			/**
			 * short -> byte[]
			 */
			Short ss=32767;
			byte[] bs=new byte[2];
			for (int j=bs.length-1;j>=0;j--) {
				bs[j]=Short.valueOf((short) (ss&0xff)).byteValue();
				ss=(short) (ss>>8);
			}
			System.out.println(Arrays.toString(bs));//[127, -1]
			
			/**
			 * byte[] -> short
			 */
			System.out.println(((bs[0] &0xff)<<8)|(bs[1]&0xff));//32767

			
			/**
			 * long -> byte[]
			 */
			Long ll=9000000000000000000L;
			byte[] bl=new byte[8];
			for (int j=7;j>=0;j--) {
				bl[j]=Long.valueOf(ll&0xff).byteValue();
				ll>>=8;
			}
			System.out.println(Arrays.toString(bl));//[124, -26, 108, 80, -30, -124, 0, 0]
			
			/**
			 * byte[] -> long
			 */
			ll=0L;
			long temp=0L;
			for (int j=0; j<8;j++) {
				temp=bl[j] & 0xff;
				ll =ll | (temp << (7-j)*8);
			}
			System.out.println(ll);//9000000000000000000


			/**
			 * byte[] -> 16进制字符串  byte2HexStr
			 */
			Byte[] bytes0=new Byte[] {15,8,9,10,41,127};//byte0[j]<128,即：数组每一项为一个字节
			StringBuilder sb=new StringBuilder();
			for(byte b:bytes0) {
				String hex=Integer.toHexString(b&0xff);
				if (hex.length()==1) {
					hex="0"+hex;
				}
				sb.append(hex.toUpperCase());
			}
			String hexStr=sb.toString();
			System.out.println(hexStr);//0F08090A297F

			/**
			 * 16进制字符串 -> byte[]  hexStr2bytes
			 */
			byte[] result=new byte[hexStr.length()/2];
			for (int j=0;j<hexStr.length()/2;j++) {//2个字符为一个字节
				int high=Integer.parseInt(hexStr.substring(j*2, j*2+1), 16);
				int low=Integer.parseInt(hexStr.substring(j*2+1, j*2+1+1), 16);
				result[j]=(byte) (high*16+low);
			}
			System.out.println(Arrays.toString(result));//[15, 8, 9, 10, 41, 127]
			
			
			/**j
		     * string2Hex  普通字符串 -> 16进制字符串
		     */
		    String str="中文Aabc";
			StringBuilder builder = new StringBuilder();
//			byte[] bytes = str.getBytes(Charset.forName("gb2312"));//D6D0CEC441616263
//			byte[] bytes = str.getBytes(Charset.forName("unicode"));//FEFF4E2D65870041006100620063
//			byte[] bytes = str.getBytes(Charset.forName("iso8859-1"));//3F3F41616263 中文无编码
			
			//String.getBytes(Charset charset)
			//static Charset forName(String charsetName)
			//charsetName:包括："gb2312"，"unicode","utf-8","iso8859-1"(纯英文)。
			byte[] bytes = str.getBytes(Charset.forName("utf-8"));//E4B8ADE6968741616263
			int bit;
			for (byte b : bytes) {
				bit = (b & 0xF0) >> 4;//high
			    //final char[] hexCode = "0123456789ABCDEF".toCharArray();
				builder.append(hexCode[bit]);
				bit = b & 0x0F;//low
				builder.append(hexCode[bit]);
			}
			System.out.println(builder.toString().trim());//D6D0CEC441616263
			
			/**
			 * hex2String  16进制字符串 -> 普通字符串  注：字符集编码必须一致。
			 */
			char[] hexs=builder.toString().toCharArray();
			byte[] bytes1=new byte[hexs.length/2];
			int n;
			for (int i=0;i<hexs.length/2;i++) {
				n=hexString.indexOf(hexs[2*i])*16;//高位字节
				n+=hexString.indexOf(hexs[2*i+1]);//低位字节
				bytes1[i]=(byte) n;
			}
			System.out.println(new String(bytes1,Charset.forName("utf-8")));//中文Aabc
			
		}
}

